export default{
	musicUpdate(){
		console.log("MusicList")
	},
	imUpdate(){
		console.log("ImUpdate")
	},
	connectSocket(that){
		var url = '';
		try {
			let value = uni.getStorageSync('serverurl')
			if (value) {
				value = value.replace("http","ws")+"/websocket/"+that.Device.deviceId
				url = value
			}
		} catch (e) {
			console.log(e)
		}
		console.log(url)
		uni.connectSocket({
			url: url,
		})
		uni.onSocketOpen(function (res) {
			that.WebSocketTool.musicUpdate()
			that.WebSocketTool.imUpdate()
		  console.log('WebSocket连接已打开！');
		})
		uni.onSocketError(function (res) {
		  console.log('WebSocket连接打开失败，请检查！');
		  setTimeout(function() {that.WebSocketTool.reConnectSocket()}, 10000);
		})
		uni.onSocketClose(function (res) {
		  console.log('WebSocket 已关闭！');
		  setTimeout(function() {that.WebSocketTool.reConnectSocket()}, 10000);
		})
		uni.onSocketMessage(function (res) {
		  console.log('收到服务器内容：' + res.data);
		  if(res.data === 'MusicList'){
			  that.WebSocketTool.musicUpdate()
		  }
		  if(res.data === 'ImMsg'){
		  	that.WebSocketTool.imUpdate()
		  }
		})
	},
	reConnectSocket(){
		var url = '';
		try {
			let value = uni.getStorageSync('serverurl')
			if (value) {
				const device = uni.getSystemInfoSync()
				value = value.replace("http","ws")+"/websocket/"+device.deviceId
				url = value
			}
		} catch (e) {
			console.log(e)
		}
		console.log(url)
		uni.connectSocket({
			url: url,
		})
	}
}